package com.ruoyi.system.service.impl;

import java.util.Iterator;
import java.util.List;

import com.ruoyi.common.core.domain.entity.Student;
import com.ruoyi.system.mapper.InstitutionStudentMapper;
import com.ruoyi.system.service.IInstitutionStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.text.Convert;

/**
 * 机构学生信息Service业务层处理
 *
 * @author ruoyi
 * @date 2021-05-26
 */
@Service
public class IInstitutionStudentServiceImpl implements IInstitutionStudentService
{
    @Autowired
    private InstitutionStudentMapper institutionStudentMapper;

    /**
     * 查询机构学生信息
     *
     * @param stuId 机构学生信息ID
     * @return 机构学生信息
     */
    @Override
    public Student selectStudentById(Long stuId)
    {
        return institutionStudentMapper.selectStudentById(stuId);
    }

    /**
     * 查询机构学生信息列表
     *
     * @param student 机构学生信息
     * @return 机构学生信息
     */
    @Override
    public List<Student> selectStudentList(Student student)
    {
        System.out.println(student.toString());
        //去掉搜索项的前后空格
        student = removeSpace(student);

        List<Student> studentList = institutionStudentMapper.selectStudentList(student);

        //性别1，0转换为男女
        Iterator it = studentList.iterator();
        while (it.hasNext()){
            Student stu = (Student) it.next();
            if (stu.getStuGender().equals("0")){
                stu.setStuGender("女");
            }
            if (stu.getStuGender().equals("1")){
                stu.setStuGender("男");
            }
        }
        return studentList;
    }

    @Override
    public List<Student> selectStudentListBySuid(Long suId) {
        return institutionStudentMapper.selectStudentListBySuid(suId);
    }

    /**
     * 去掉多余的空白字符
     *
     * @param student 学生信息
     * @return 学生对象
     */
    private Student removeSpace(Student student) {
        //姓名
        if (student.getStuName() != null){
            student.setStuName(student.getStuName().replace(" ",""));
        }

        //年级
        if (student.getStuGrade() != null){
            student.setStuGrade(Long.parseLong(String.valueOf(student.getStuGrade()).replace(" ","")));
        }

        //学校
        if (student.getStuSchool() != null){
            student.setStuSchool(student.getStuSchool().replace(" ",""));
        }

        //年纪
        if (student.getStuAge() != null){
            student.setStuAge(student.getStuAge().replace(" ",""));
        }
        return student;
    }

    /**
     * 新增机构学生信息
     *
     * @param Student 机构学生信息
     * @return 结果
     */
    @Override
    public int insertStudent(Student Student)
    {
        return institutionStudentMapper.insertStudent(Student);
    }

    /**
     * 修改机构学生信息
     *
     * @param Student 机构学生信息
     * @return 结果
     */
    @Override
    public boolean updateStudent(Student Student)
    {
        return institutionStudentMapper.updateStudent(Student);
    }

    /**
     * 删除机构学生信息对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteStudentByIds(String ids)
    {
        return institutionStudentMapper.deleteStudentByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除机构学生信息信息
     *
     * @param stuId 机构学生信息ID
     * @return 结果
     */
    @Override
    public int deleteStudentById(Long stuId)
    {
        return institutionStudentMapper.deleteStudentById(stuId);
    }
}

